{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "1、生成RSA密钥对",
   "id": "f2f49cf4b02d75cf"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T05:54:48.704227Z",
     "start_time": "2025-03-03T05:54:44.863955Z"
    }
   },
   "cell_type": "code",
   "source": "pip install cryptography",
   "id": "afb79c1f4eb6b359",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple\n",
      "Collecting cryptography\n",
      "  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/33/cf/1f7649b8b9a3543e042d3f348e398a061923ac05b507f3f4d95f11938aa9/cryptography-44.0.2-cp39-abi3-win_amd64.whl (3.2 MB)\n",
      "     ---------------------------------------- 0.0/3.2 MB ? eta -:--:--\n",
      "     ------------- -------------------------- 1.0/3.2 MB 5.6 MB/s eta 0:00:01\n",
      "     ----------------------------- ---------- 2.4/3.2 MB 5.8 MB/s eta 0:00:01\n",
      "     ---------------------------------------- 3.2/3.2 MB 5.9 MB/s eta 0:00:00\n",
      "Requirement already satisfied: cffi>=1.12 in d:\\soft\\anaconda\\anaconda3\\envs\\microservice\\lib\\site-packages (from cryptography) (1.17.1)\n",
      "Requirement already satisfied: pycparser in d:\\soft\\anaconda\\anaconda3\\envs\\microservice\\lib\\site-packages (from cffi>=1.12->cryptography) (2.21)\n",
      "Installing collected packages: cryptography\n",
      "Successfully installed cryptography-44.0.2\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T05:55:23.823731Z",
     "start_time": "2025-03-03T05:55:23.583514Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from cryptography.hazmat.primitives.asymmetric import rsa\n",
    "from cryptography.hazmat.primitives import serialization\n",
    "\n",
    "# 生成RSA私钥\n",
    "private_key = rsa.generate_private_key(\n",
    "    public_exponent=65537,  # 常用的公钥指数\n",
    "    key_size=2048          # 密钥长度为2048位\n",
    ")\n",
    "\n",
    "# 生成公钥\n",
    "public_key = private_key.public_key()\n",
    "\n",
    "# 将私钥导出为PEM格式\n",
    "private_pem = private_key.private_bytes(\n",
    "    encoding=serialization.Encoding.PEM,\n",
    "    format=serialization.PrivateFormat.TraditionalOpenSSL,\n",
    "    encryption_algorithm=serialization.NoEncryption()  # 不加密私钥\n",
    ")\n",
    "\n",
    "# 将公钥导出为PEM格式\n",
    "public_pem = public_key.public_bytes(\n",
    "    encoding=serialization.Encoding.PEM,\n",
    "    format=serialization.PublicFormat.SubjectPublicKeyInfo\n",
    ")\n",
    "\n",
    "# 打印公钥和私钥（PEM格式字符串）\n",
    "print(\"Public Key (PEM):\\n\", public_pem.decode('utf-8'))\n",
    "print(\"\\nPrivate Key (PEM):\\n\", private_pem.decode('utf-8'))"
   ],
   "id": "8a1dbe97aca48873",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Public Key (PEM):\n",
      " -----BEGIN PUBLIC KEY-----\n",
      "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1e7wPl8C3gikfxRGMmqj\n",
      "6it8e2IN7G57ysA6jOVxw0EUl5Zf0rBIOThPa+5APTl2rHW0gfRpQKWKUFU8Ouk2\n",
      "WnXGmW6UZb16gmHoeVOIphskcNgODkEcaLoHIXwtDTGcqBgssNhOFEkT7Em3h1P7\n",
      "W0pSeqAjgU6kEKcoRhKZwfptBTV3SC5sTj5ZdYz9D9liJlGLFbIepQPG53goU/Cy\n",
      "FB2eUHWm6OmREnrQaqbIKWuI6+9G54SlfICRpPv6XJaBSlPpVyaRH1CmPKILvutf\n",
      "wJg/w3PhvX0BaBCDl0DGT6jO/WF9ENcOdCU7OLxLfSIfdL14jQ2IdPTAt+tJfOFh\n",
      "dwIDAQAB\n",
      "-----END PUBLIC KEY-----\n",
      "\n",
      "\n",
      "Private Key (PEM):\n",
      " -----BEGIN RSA PRIVATE KEY-----\n",
      "MIIEowIBAAKCAQEA1e7wPl8C3gikfxRGMmqj6it8e2IN7G57ysA6jOVxw0EUl5Zf\n",
      "0rBIOThPa+5APTl2rHW0gfRpQKWKUFU8Ouk2WnXGmW6UZb16gmHoeVOIphskcNgO\n",
      "DkEcaLoHIXwtDTGcqBgssNhOFEkT7Em3h1P7W0pSeqAjgU6kEKcoRhKZwfptBTV3\n",
      "SC5sTj5ZdYz9D9liJlGLFbIepQPG53goU/CyFB2eUHWm6OmREnrQaqbIKWuI6+9G\n",
      "54SlfICRpPv6XJaBSlPpVyaRH1CmPKILvutfwJg/w3PhvX0BaBCDl0DGT6jO/WF9\n",
      "ENcOdCU7OLxLfSIfdL14jQ2IdPTAt+tJfOFhdwIDAQABAoIBAEuX5glL/lHhoOqM\n",
      "ie9nsirdGdvpPn1EXva6LRY7PxMc7H9R8eMbSjH53UqIgeeAZhgzcUiYSha/R9vx\n",
      "+lngrUWSp03w/nvi06a9nO0+IKN9JMfA+Fp+FGFHFpz06WBnnvDgte35SMGvPtx4\n",
      "AeNOQBu2a+WLr3ASDzQZNMhp6y3Yv2ytAE81xTrArCZJnHp6zylTcIIMJN2G4d3o\n",
      "a0ZuSBBzmnO0s7Gi35JyK7yxSSdrkOEb4wJV2d2WZiJDkJxb584u7HSoJ61IEcaF\n",
      "+FDGiSzyVd+/d3RqqV33qU/XINtM8efllDJhir63K8rFX/eBXCsbfbQZyapiwecz\n",
      "ayDblAkCgYEA8sFnuBlVFd+NFz6aukKHN/qf4WEcY9e3a5WzXPd2kp27LT7Xaru8\n",
      "Q9EhoptjxuyySh9iGJxkEo3kiqte1qUu1extR2WNDSrW6hb/nGqgKBlSe91ggDTP\n",
      "Tg8D2bOvC+6Le5WoQahXuwBWWVznVpJ85MQEGSGdFXZoanYBzXhlZ2kCgYEA4Zr4\n",
      "mcQTcSVZ0oM0XBr18GWrX+YMMAqBboHZHzwzvjSyCW1humPhbOEb0rnD+2uN996z\n",
      "Tquae7wA5jT/ZqGupmhjqmZUtm59Sz/gMDtOZWuSQUIsqTKPlC13Qucseyh6UkvB\n",
      "WF897ikYfA5qnlv91L5yYZ5aF/ToyNRF7nmlRd8CgYEAzGgufcZW7uQeWcRNZSk/\n",
      "0tMmbRDoZunteJnFPjLhlPNb+O1BVo5eKHygZduSWJ5WQjPK1g5Z9YWlO16xrOvK\n",
      "VDeQjN3lt4CrFtHmyj6+PQWVftcZutq89G2QQHAcpV9n89hApYiq1ruh+f1QjoUq\n",
      "xTaIfODg9IGPypUzT2BhgoECgYBsmsrEUxS86iskYQ8Kq9vrPzZRsw/47IXEafPY\n",
      "yoVqMjnmueZK8Rwq6u8W8WB8Hxm30lhlLczvdeDE8Yxlnr3R0/zuodZ0ABqG6BgH\n",
      "f/VMc41zpuSTCwj9XynDZkAOhbzk8xz/atIIXXYw+kuvMj+uc4PipHVghQCu9Sk5\n",
      "FQxP5wKBgAp7tyluVP2cs7qsRZ6CGA938zHXbctlJKj6Ttx2CPhj5C1nTcm7KtWE\n",
      "ZWHXPFuXwWNUdoybsYojV2uElijmTfoMoL5p+uMMwavJqvGlE87B/qDX7DpsiAgO\n",
      "I2+MfwgZUpX9ICuZT1hp5+/P7M9RBIN/odi7xQeeLZwrhxEcRA03\n",
      "-----END RSA PRIVATE KEY-----\n",
      "\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "2、调试秘钥",
   "id": "2f6219916c29e91f"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "2.1 保存已设置的公钥（尘锋）",
   "id": "25daf4c0fba4a7b2"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "-----BEGIN PUBLIC KEY-----\n",
    "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAv/NfO4Nn/tG4f4cxXm9i\n",
    "7NeI+LEZpBK10nx5mUgEkZKEOHULMbGUkuAtp0jxwQ2vKrOSv+dKz6RZodmw1YtT\n",
    "zA4VelgWFrHD2LZwa7X7UCTF7Sd5pOgd1WHYJyF2JvHWW1JrDtgatZz8xd2iIL6q\n",
    "4+92PVHBXoyGEQ864pXTbRRuqWuVoKqSJHjdzRslEnp71o2pTv49gIjVcqzF2mGO\n",
    "K4Mm3nAavsFgeaM0MULpogTE4MC6pBUmFKHn72gfKvLoQKuwclScIUAaQLQo8PuP\n",
    "XZTbyoC10T1gbHCCxMUhK185KqWosuPd0SbpoacxiRTKPXa9+6RGKBAFC2ye0Qwx\n",
    "SQIDAQAB\n",
    "-----END PUBLIC KEY-----"
   ],
   "id": "725b270d05f7073c"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "3、使用Nginx和uWSGI部署Django项目",
   "id": "8ab1eb730528c7e4"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "3.1 查看Nginx服务是否启动",
   "id": "be355b033c949346"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "ps aux|grep nginx",
   "id": "e0319662d851940b"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "",
   "id": "ac3c8e84d07c87e5"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
